package com.xin2.dao;

import comxin.demo1.Utils.JdbcUtils;

import java.lang.reflect.Field;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public abstract class BaseDao {
    /*
          增删改的通用操作
     */
    public  int update(Connection conn,String sql,Object...args){
        PreparedStatement  ps=null;
        try {
            ps=conn.prepareStatement(sql);
            for (int i=0;i< args.length;i++){
                ps.setObject(i+1,args[i]);
            }
            return  ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JdbcUtils.close(null,ps);     //注意这里没有关闭连接
        }
        return 0;
    }

    /*
         查询操作，返回数据表中的一行数据
     */
    public <T> T getInstance(Connection conn,Class<T> clazz, String sql,Object...args){
        PreparedStatement ps=null;
        ResultSet re=null;
        try {
            ps=conn.prepareStatement(sql);
            for (int i=0;i< args.length;i++){
                ps.setObject(i+1,args[i]);
            }
            re = ps.executeQuery();
            ResultSetMetaData rsmd = re.getMetaData();
            int columnCount=rsmd.getColumnCount();
            if(re.next()){
                T t= clazz.newInstance();    //这行代码的意思是什么
                for(int i=0;i<columnCount;i++){
                    Object columValue = re.getObject(i + 1);
                    String columnLabel = rsmd.getColumnLabel(i + 1);
                    Field field = clazz.getDeclaredField(columnLabel); //以下代码不太懂
                    field.setAccessible(true);
                    field.set(t,columValue);
                }
                return t;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JdbcUtils.close(null,ps,re);
        }
        return null;
    }

    /*
          通用的查询操作，返回查询后的多条集合
     */
    public <T> List<T> getForlist(Connection conn, Class<T> clazz, String sql){
        PreparedStatement ps=null;
        ResultSet re=null;
        try {
            ps=conn.prepareStatement(sql);
            re= ps.executeQuery();
            ResultSetMetaData rsmd = re.getMetaData();
            int columnCount = rsmd.getColumnCount();
            ArrayList<T> list=new ArrayList<>();
            while (re.next()){
                T t=clazz.newInstance();
                for(int i=0;i<columnCount;i++){
                    Object columnValue = re.getObject(i + 1);
                    String columnLabel = rsmd.getColumnLabel(i + 1);
                    Field field = clazz.getDeclaredField(columnLabel);
                    field.setAccessible(true);
                    field.set(t,columnValue);
                    list.add(t);
                }
            }
            return list;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JdbcUtils.close(null,ps,re);
        }
        return null;
    }

    /*
             查询特殊值的通用方法
     */
    public <E> E getValue(Connection conn,String sql){
       PreparedStatement ps=null;
       ResultSet re=null;
        try {
            ps=conn.prepareStatement(sql);
            re = ps.executeQuery();
            if(re.next()){
                return (E) re.getObject(1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JdbcUtils.close(null,ps,re);
        }
        return null;
    }



}
